package cn.platform.controller;

import cn.platform.entity.Teacher;
import cn.platform.request.*;
import cn.platform.response.*;
import cn.platform.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 用户管理，教师条件分页查询
     * @param userTeacherQueryDTO
     * @return
     */
    @GetMapping("/teacher")
    public Result page(UserTeacherQueryDTO userTeacherQueryDTO){
        log.info("用户管理-教师条件分页查询");
        PageResult<UserTeacherVO> pageResult = userService.pageTeacher(userTeacherQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 用户管理，新增教师
     * @param teacher
     * @return
     */
    @PostMapping("/teacher")
    public Result saveTeacher(@RequestBody Teacher teacher){
        log.info("新增教师{}",teacher);
        userService.saveTeacher(teacher);
        return Result.success();
    }

    /**
     * 用户管理，查看教师详细信息
     * @param id
     * @return
     */
    @GetMapping("teacher/{id}")
    public Result teacherInfo(@PathVariable Integer id){
        log.info("根据id查看教师详细信息：{}",id);
        UserTeacherVO teacher = userService.getTeacherInfo(id);
        return Result.success(teacher);
    }

    /**
     * 用户管理，更新教师的信息
     * @param id
     * @param teacher
     * @return
     */
    @PutMapping("/teacher/{id}")
    public Result updateTeacher(@PathVariable Integer id,@RequestBody Teacher teacher){
        log.info("更新教师信息{}",teacher);
        teacher.setId(id);
        userService.updateTeacher(teacher);
        return Result.success();
    }

    /**
     * 用户管理，根据id删除老师
     * @param id
     * @return
     */
    @DeleteMapping("/teacher/{id}")
    public Result deleteTeacher(@PathVariable Integer id){
        log.info("根据id{}删除教师",id);
        userService.deleteTeacherById(id);
        return Result.success();
    }

    /**
     * 用户管理，根据id批量删除教师信息
     * @param ids
     * @return
     */
    @PostMapping("/teacher/batch")
    public Result batchDeleteTeacher(@RequestBody List<Integer> ids){
        log.info("根据id{}批量删除教师",ids);
        userService.batchDeleteTeacherById(ids);
        return Result.success();
    }

    /**
     * 分页查询学生信息
     * @param userStudentQueryDTO
     * @return
     */
    @GetMapping("/student")
    public Result pageStudent(UserStudentQueryDTO userStudentQueryDTO){
        log.info("分页查询学生信息{}",userStudentQueryDTO);
        PageResult<UserStudentVO> pageResult =userService.pageStudent(userStudentQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 用户管理，新增学生信息
     * @param userStudentDTO
     * @return
     */
    @PostMapping("/student")
    public Result saveStudent(@RequestBody UserStudentDTO userStudentDTO){
        log.info("新增学生{}",userStudentDTO);
        userService.saveStudent(userStudentDTO);
        return Result.success();
    }

    /**
     * 用户管理更新学生信息
     * @param id
     * @param userStudentDTO
     * @return
     */
    @PutMapping("/student/{id}")
    public Result updateStudent(@PathVariable Integer id, @RequestBody UserStudentDTO userStudentDTO){
        log.info("更新学生信息{}",userStudentDTO);
        userStudentDTO.setId(id);
        userService.updateStudent(userStudentDTO);
        return Result.success();
    }

    /**
     * 用户管理，删除单条学生信息
     * @param id
     * @return
     */
    @DeleteMapping("/student/{id}")
    public Result deleteStudentById(@PathVariable Integer id){
        log.info("删除学生信息{}",id);
        userService.deleteStudent(id);
        return Result.success();
    }

    /**
     * 用户管理，批量删除学生信息
     * @param ids
     * @return
     */
    @PostMapping("/student/batch")
    public Result batchDeleteStudent(@RequestBody List<Integer> ids){
        log.info("批量删除学生信息{}",ids);
        userService.batchDeleteStudentById(ids);
        return Result.success();
    }

    /**
     * 根据id获取学生姓名
     * @param studentId
     * @return
     */
    @GetMapping("/students/{studentId}")
    public Result getStudentNameById(@PathVariable Integer studentId){
        log.info("根据id获取学生姓名");
        String realName =userService.getStudentNameById(studentId);
        return Result.success(realName);
    }

    /**
     * 用户管理，分页查询
     * @param queryDTO
     * @return
     */
    @GetMapping("/parent")
    public Result pageParent(UserParentQueryDTO queryDTO){
        log.info("分页查询学生信息{}",queryDTO);
        PageResult<UserParentVO> pageResult = userService.pageParent(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增家长信息
     * @param userParentDTO
     * @return
     */
    @PostMapping("/parent")
    public Result saveParent(@RequestBody UserParentDTO userParentDTO){
        log.info("新增家长信息{}",userParentDTO);
        userService.saveParent(userParentDTO);
        return Result.success();
    }

    /**
     * 更新家长信息
     * @param id
     * @param userParentDTO
     * @return
     */
    @PutMapping("/parent/{id}")
    public Result updateParent(@PathVariable Integer id ,@RequestBody UserParentDTO userParentDTO){
        log.info("更新家长的信息{}",userParentDTO);
        userService.updateParent(userParentDTO);
        return Result.success();
    }

    @DeleteMapping("/parent/{id}")
    public Result deleteParent(@PathVariable Integer id){
        log.info("删除单个家长{}",id);
        userService.deleteParent(id);
        return Result.success();
    }

    @PostMapping("/parent/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        log.info("批量删除家长{}",ids);
        userService.batchDeleteParentById(ids);
        return Result.success();
    }

}
